﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using zdzh.zdmgr.BLL;
using zdzh.zdmgr.COL;
using zdzh.zdmgr.SFL;

namespace zdzh.zdmgr.UIL.fangyuanp
{
    /// <summary>
    /// 对象名称：“回迁协议模块”列表页面（用户界面层）
    /// 对象说明：该页面显示所有“回迁协议”的信息列表，可以通过该界面导航到相应的添加、修改、删除页面。
    /// 作者姓名：飞翔熊
    /// 编写日期：2012/12/3 13:23:16
    /// </summary>
    public partial class Default : System.Web.UI.Page
    {
        /// <summary>
        /// “回迁协议模块”列表页面载入时的事件处理方法。
        /// 指定PageBar分页控件所关联的数据显示控件、数据源，执行数据绑定方法。
        /// </summary>
        private List<Fangyuan> fangyuan;
        private List<string> buildlist;     //  楼号
        private List<string> unitlist;      //  单元
        private List<string> floorlist;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                buildlist = FangyuanBLL.GetBuildList();
                DdlBuild.DataSource = DataValid.SortBuild(buildlist);
                DdlBuild.DataBind();
                DdlBuild_SelectedIndexChanged(null, null);
                //BindObjectToForm();  // 将关联对象当前值，绑定至界面控件进行显示。
            }
        }
        
        protected void DdlBuild_SelectedIndexChanged(object sender, EventArgs e)
        {
            fangyuan = FangyuanBLL.GetDataByBuild(DdlBuild.SelectedItem.Value); // 从数据库中获取关联对象信息，以备进行修改操作。
            if ( DataValid.IsNull(fangyuan) )
                throw new CustomException("对不起，所要修改的信息不存在或已删除。");
            unitlist = FangyuanBLL.GetUnitList(DdlBuild.SelectedItem.Value);
            floorlist = FangyuanBLL.GetFloorList(DdlBuild.SelectedItem.Value, "1");
            floorlist = DataValid.SortFloor(floorlist);
        }

        protected List<Fangyuan> GetFangyuan(string build, string unit, string floor)
        {
            List<Fangyuan> list = new List<Fangyuan>();
            foreach (Fangyuan fang in fangyuan)
            {
                if (fang.Build != build || fang.Unit != unit || fang.Floor != floor)
                    continue;
                list.Add(fang);
            }
            return list;
        }

        protected string GetFangyuan()
        {
            string ret = "<table class=\"TableData\"><colgroup>";
            for (int i = 0; i < unitlist.Count; i++)
            {
                ret += "<col width=\"200\" />";
            }
            ret+="</colgroup>";
            List<Fangyuan> list;
            for (int i = 0; i <floorlist.Count; i++)
            {
                ret += "<tr class=\"TableRow\">";
                for (int m = 0; m < unitlist.Count; m++)
                {
                    list = GetFangyuan(DdlBuild.SelectedItem.Value, unitlist[m], floorlist[i]);
                    ret += "<td>";
                    ret += "<table class=\"TableData\"><tr class=\"TableRow\">";
                    for (int n = 0; n < list.Count; n++)
                    {
                        ret += "<td>";
                        Fangyuan fang = list[n];
                        ret += "<a href=\"Detail.aspx?Index=" + fang.Index.ToString() + "\">";
                        ret += fang.getRoom();
                        ret += "</a>";
                        if ( fang.XieyiIndex==null||fang.XieyiIndex==0)
                            ret+="<img src=\"../Images/NotNull.png\" alt=\"NotNull\"/>";
                        ret += "<br>" + fang.Area.ToString() + "㎡";
                        ret += "</td>";
                    }
                    ret += "</tr></table>";
                }
                ret += "</tr>";
            }
            ret += "<tr>";
            for (int i = 0; i < unitlist.Count; i++)
            {
                ret += "<td>" + (i + 1).ToString() + "单元</td>";
            }
            ret += "</tr></table><br><br>";

            list = GetFangyuan(DdlBuild.SelectedItem.Value, null, null);
            if (list.Count > 0)
            {
                int rowscount = 10;
                ret += "<table class=\"TableData\"><tr>";
                if (list.Count>rowscount)
                {
                    rowscount = list.Count / 2;
                    if (list.Count % 2 != 0)
                        rowscount += 1;
                }

                for (int i = 0; i < list.Count;i++ )
                {
                    if (i==rowscount)
                        ret += "</tr><tr>";
                    Fangyuan fang = list[i];
                    ret += "<td>" + "<a href=\"Detail.aspx?Index=" + fang.Index.ToString() + "\">" + fang.Room + " " + fang.getType() + "</a>";
                    if (fang.XieyiIndex == null || fang.XieyiIndex == 0)
                        ret += "<img src=\"../Images/NotNull.png\" alt=\"NotNull\"/>";
                    ret += "<br>（" + fang.Area + "㎡）";
                    ret += "</td>";
                }
                ret += "</tr></table>";
            }


            return ret;
        }
    }
}
